-- King
-- Created by chengb Jul/26/2016
-- 国王格子

local KING_PET_ID = 20;    -- 国王对应的冈布奥编号

-- 内部函数声明
local showSubmitForm;
local talkFirstTime;
local talkToRescue;
local talkAfterDefeatDragon;
local talkAndBonus;
local takeForSmallBadge;
local ownedSmallBadge;

-- 初始化
function initKing(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local iconPath = getHeroIconPath(KING_PET_ID);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            if not BraveVSDragonM.hasTalkedWithRole(self.gridData.type, 1) then
                -- 和国王首次对话
                talkFirstTime(self);
            elseif not BraveVSDragonM.hasRescuedPrincess() then
                -- 还没救回公主
                talkToRescue(self);
            elseif BraveVSDragonM.hasDefeatedDragon() and BraveVSDragonM.ownedLightJade() then
                -- 已经击败龙王，并且持有光之玉
                talkAfterDefeatDragon(self);
            elseif not BraveVSDragonM.hasTalkedWithRole(self.gridData.type, 2) then
                -- 还没触发过小徽章的对话
                takeForSmallBadge(self);
            elseif ownedSmallBadge(self) and not BraveVSDragonM.hasGotBuildingBonus(self.gridData.class) then
                -- 持有小徽章，并且没有领取国王的奖励
                talkAndBonus(self);
            else
                -- 显示小徽章提交窗口
                showSubmitForm(self, iconPath);
            end
        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.icon:setScale(1.2);
    self.item:setPositionX(-5);
    self.item:setPositionY(-5);

    -- 呼吸
    self.needAnimation = true;
end

-- 是否持有小徽章
function ownedSmallBadge(self)
    local elementDbase = DungeonM.queryElement(self.gridData.element, "dbase");
    local classId = elementDbase["submit_item"];
    return ItemM.getAmount(ME.user, classId) > 0;
end

-- 和国王首次对话
function talkFirstTime(self)
    DungeonActionM.go("talk_with_brave_role", self.gridData:getPos(), 1);

    -- 触发对话
    startDialogueTemplate("king_talk1");
end

-- 还没救出公主时的对话
function talkToRescue(self)
    startDialogueTemplate("king_talk1");
end

-- 击败龙王之后的对话
function talkAfterDefeatDragon(self)
    local function callback()
        -- 领取击败龙王的奖励
        local bonus = FormulaM.invoke("CALC_DEFEAT_DARGON_BONUS");
        if DungeonActionM.go("take_defeat_dragon_bonus", self.gridData:getPos()) then
            self:playBonusListEffect(bonus, false);
        end
    end
    startDialogueTemplate("king_talk_defeated_dragon", callback);
end

-- 对话完后给予国王的奖励
function talkAndBonus(self)
    local function endCallback()
        DungeonActionM.go("brave_building_bonus", self.gridData:getPos());
    end

    -- 触发对话
    startDialogueTemplate("king_talk4", endCallback);
end

-- 公主出现后触发小徽章的对话
function takeForSmallBadge(self)
    DungeonActionM.go("talk_with_brave_role", self.gridData:getPos(), 2);

    startDialogueTemplate("king_talk3");
end

-- 显示小徽章提交窗口
function showSubmitForm(self, iconPath)
    local function okFunc()
        if BraveVSDragonM.allSmallBadgeBonusTaken() then
            -- 所有小徽章奖励都已领取
            alert(getLocStr("all_small_badge_bonus_taken"));
            return;
        end

        if not ownedSmallBadge(self) then
            -- 没有小徽章
            alert(getLocStr("lack_submit_item"));
            return;
        end

        -- 提交小徽章
        DungeonActionM.go("submit_small_badge", self.gridData:getPos())
    end

    self:openBuildingView(okFunc, nil, nil, nil, nil, iconPath);
end

-- 救出公主后国王发言
function doKingTalkRescue(self)
    local function endCallback()
        -- 公主出现
        DungeonActionM.go("princess_appear", self.gridData:getPos())
    end

    -- 触发对话
    startDialogueTemplate("king_talk2", endCallback);
end
